package com.yuju.hotel.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.StatusEnum;
import com.yuju.hotel.dao.IRoomRepairDao;
import com.yuju.hotel.dao.entity.RoomRepair;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.model.RoomRepairModel;
import com.yuju.hotel.service.IRoomRepairService;
import com.yuju.hotel.service.IRoomService;
import com.yuju.util.Utils;

/**
 * 
 * 类名：RoomRepairRepairServiceImpl  <br />
 *
 * 功能：
 *
 * @author : zhousc <br />
 * @Date : 2017年11月30日 下午2:00:39  <br />
 * @version : 1.1 <br />
 */
@Service
public class RoomRepairServiceImpl extends AbstractBaseService<RoomRepairModel, RoomRepair, String> implements IRoomRepairService{

	@Autowired
	private IRoomRepairDao roomRepairDao;
	@Autowired
	private  IRoomService roomService;
	
	
	@Override
	public IBaseDao<RoomRepairModel, RoomRepair, String> getDao() {
		// TODO Auto-generated method stub
		return roomRepairDao;
	}
	/**
	 * 
	 * 功能: 新增维修信息<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月30日 下午2:01:31<br/>
	 * 
	 * @param roomRepairModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int save(RoomRepairModel roomRepairModel){
		roomRepairModel.setId(Utils.getUUID());
		roomRepairModel.setRoomId(roomRepairModel.getRoomId());
		roomRepairModel.setGmtCreate(new Date());
		roomRepairModel.setGmtModified(new Date());
		this.getDao().saveSelective(roomRepairModel);
		return 1;
	}
	
	/**
	 * 
	 * 功能: 更新维修信息<br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年11月30日 下午2:01:57<br/>
	 * 
	 * @param roomRepairModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#updateSelective(java.lang.Object)
	 */
	@Override
	public int updateSelective(RoomRepairModel roomRepairModel){
		roomRepairModel.setGmtModified(new Date());
		return this.getDao().updateSelective(roomRepairModel);
	}
	/**
	 * 
	 * 功能:维修完成 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2017年12月15日 上午11:31:54<br/>
	 * 
	 * @param roomRepairModel
	 * @return <br/>
	 * @see com.yuju.hotel.service.IRoomRepairService#finishRepair(com.yuju.hotel.model.RoomRepairModel)
	 */
	@Override
	public int finishRepair(RoomRepairModel roomRepairModel) {
		//查询此条维修信息，维修完成后是否置为脏房
		RoomRepairModel model = new RoomRepairModel();
		model.setRoomId(roomRepairModel.getRoomId());
		model.setRepairStatus((byte)1);
		Map<Object, Object> beanMap = getBeanMap(model);
		List<RoomRepairModel> list = this.getDao().list(beanMap);
		//1 是设置为脏房
		if(list.get(0).getIsSetDirty() == 1){
			RoomModel roomModel = new RoomModel();
			roomModel.setId(roomRepairModel.getRoomId());
			roomModel.setIsDirty(StatusEnum.TRUE.getValue());
			roomService.updateSelective(roomModel);
		}
		
		//更新维修状态
		roomRepairModel.setRepairStatus(StatusEnum.FALSE.getValue());
		//更新结束时间
		roomRepairModel.setEndDay(new Date());
		roomRepairModel.setGmtModified(new Date());
		this.getDao().updateSelective(roomRepairModel);
		return 1;
	}
	/**
	 * 
	 * 功能：房态查询房间是否维修状态<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月16日 上午11:49:13 <br/>
	 */
	@Override
	public Byte selectRepairStatus(RoomRepairModel roomRepairModel) {
		
		RoomRepairModel model = new RoomRepairModel();
		model.setRoomId(roomRepairModel.getRoomId());
		model.setRepairStatus((byte)1);
		model.setCurrentDate(new Date());
		int selectRepairStatus = roomRepairDao.selectRepairStatus(model);
		//如果没有查询出维修记录 返回1
		if(selectRepairStatus == 0){
			//未维修状态
			return StatusEnum.FALSE.getValue();
		}
		//维修状态
		return StatusEnum.TRUE.getValue();
	}
}